<?php
namespace App\HttpController\Api;

use App\Lib\Check\Authorize;
use App\Repositories\Eloquent\Api\AppidModel;
use EasySwoole\Http\AbstractInterface\Controller;
use EasySwoole\Http\Message\Status;

class Jwt extends Controller  {

    use Authorize;
    protected $_model_appid;


    /**
     * @Api(name="获取jwt",group="接口验证",path="/api/jwt",description="获取jwt接口")
     * @Method(allow={GET})
     * @Param(name="appid",required="true")
     * @Param(name="user",required="")
     * @ApiRequestExample(curl http://180.76.174.107/api/example/jwt?appid=c128711c12efbfc403b519bf31dd1b8e&user=asldkfjalsfjalsfjlsfjasldfj)
     * @ApiSuccess(
              {
                "code": 200,
                "msg": "OK",
                "count": 0,
                "data": {
                "appid": "c128711c12efbfc403b519bf31dd1b8e",
                "jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODg5OTQwMDMsInN1YiI6ImVhc3lzd29vbGUtYXBpIiwibmJmIjoxNTg4OTkzNzAzLCJhdWQiOiIiLCJpYXQiOjE1ODg5OTM3MDMsImp0aSI6IjFmMzJmNzI3NTRmMDI5ZDljZDBmYWY5MjBmOTkxYjU5Iiwic3RhdHVzIjoxLCJkYXRhIjp7InZlcnNpb24iOiIxLjI1IiwiY2hhbm5lbCI6Imh1YXdlaSIsImRpZmEiOiIxMjNqZmFzZnNsaiJ9fQ.fwqXne3M4az_DjdqGPiyEX7L406EQmNj9xFpwM0Xw0c"
                }
              }
      )
     * @ResponseParam(name="code",description="状态码")
     * @ResponseParam(name="count",description="条数")
     * @ResponseParam(name="msg",description="api提示信息")
     * @ResponseParam(name="data",type=array,description="返回数据")
     * @ResponseParam(name="--appid",description="appid")
     * @ResponseParam(name="--jwt",description="jwt")
     */
    public function createJwt(){

        $this->_model_appid = new AppidModel();
        $appid = $this->request()->getRequestParam('appid');
        $user = $this->request()->getRequestParam('user');
        $user = $user??USER_SESSION_NOT_LOGIN;
        $appsecret = $this->_model_appid->getAppsecretByAppid($appid);
        $this->setConfig($appid,$appsecret);
        $jwt = $this->createToken($user,'1.25','huawei','123jfasfslj');
        $res = [];
        $res['code'] = 200;
        $res['msg'] = Status::getReasonPhrase(200);
        $res['data'] = ['appid'=>$appid,'jwt'=>$jwt];
        $this->response()->withHeader('Content-type', 'application/json;charset=utf-8');
        $this->response()->write(json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
        $this->response()->end();

    }


    protected function gc()
    {
        parent::gc(); // TODO: Change the autogenerated stub
        $this->_model_appid = null;
    }


}
